home *** CD-ROM | disk | FTP | other *** search
/ Windows 95 API Bible / Windows 95 API Bible 3 Disc Set.iso / Win32 API Bible Book 1 of 3 / CHAPTER4 / ADJWNDEX.C next >
C/C++ Source or Header  |  1996-01-06  |  6KB  |  184 lines

  1.  
  2. #include <windows.h>  
  3. #include "AdjWndEx.h" 
  4.  
  5.  
  6. #if defined (WIN32)
  7.     #define IS_WIN32 TRUE
  8. #else
  9.     #define IS_WIN32 FALSE
  10. #endif
  11.  
  12. #define IS_NT      IS_WIN32 && (BOOL)(GetVersion() < 0x80000000)
  13. #define IS_WIN32S  IS_WIN32 && (BOOL)(!(IS_NT) && (LOBYTE(LOWORD(GetVersion()))<4))
  14. #define IS_WIN95   (BOOL)(!(IS_NT) && !(IS_WIN32S)) && IS_WIN32
  15.  
  16. HWND      hWnd = NULL; 
  17. HINSTANCE hInst;   // current instance
  18.  
  19. LPCTSTR lpszAppName   = "MyApp";
  20. LPCTSTR lpszClassName = "My Class";
  21.  
  22. BOOL InitAppInstance( HINSTANCE hInstance );
  23. BOOL RegisterWin95( CONST WNDCLASS* lpwc );
  24.  
  25. int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
  26.                       LPTSTR lpCmdLine, int nCmdShow)
  27. {
  28.     MSG  msg;
  29.     RECT rect;
  30.  
  31.     // Check to see if there is previous version of the application
  32.     // currently running. If not, then register the application class.
  33.     //................................................................
  34.     if ( !hPrevInstance && !InitAppInstance( hInstance ) )
  35.         return FALSE;
  36.  
  37.     hInst = hInstance;
  38.  
  39.     // Initialize a RECT with the size and position of 
  40.     // the client area we want.
  41.     //...................................
  42.     
  43.     rect.left   = 100; // 100 pixels from the left of the screen.
  44.     rect.top    = 150; // 150 pixels from the top of the screen.
  45.     rect.right  = 200; // The width will be 100 pixels.
  46.     rect.bottom = 200; // the height will be 50 pixels.
  47.  
  48.     AdjustWindowRectEx( &rect, WS_OVERLAPPEDWINDOW, TRUE, WS_EX_TOOLWINDOW    );
  49.  
  50.     hWnd = CreateWindowEx( WS_EX_TOOLWINDOW,  // Create a window with a small caption.
  51.                            lpszClassName,      // Class name registered.
  52.                            "My Application",    // Title bar text.
  53.                            WS_OVERLAPPEDWINDOW,// Window style.
  54.                            rect.left,          // Use the rect calculated
  55.                            rect.top,           //   with AdjustWindowRect()
  56.                            rect.right,         //   for the size and
  57.                            rect.bottom,        //   position.
  58.                            NULL,               // No parent.
  59.                            NULL,               // Use class menu.
  60.                            hInstance,           
  61.                            NULL );               
  62.  
  63.     if ( !hWnd )
  64.        return( FALSE );
  65.  
  66.     ShowWindow( hWnd, nCmdShow ); 
  67.     UpdateWindow( hWnd );         
  68.  
  69.     while ( GetMessage( &msg, NULL, 0, 0 ) )
  70.     {
  71.        TranslateMessage(&msg);
  72.        DispatchMessage(&msg); 
  73.     }
  74.     
  75.     return( TRUE );
  76. }
  77.  
  78.  
  79. BOOL InitAppInstance( HINSTANCE hInstance )
  80. {
  81.    WNDCLASS wc;
  82.  
  83.    // Register the main application window class.
  84.    //............................................
  85.    wc.style         = CS_HREDRAW | CS_VREDRAW;
  86.    wc.lpfnWndProc   = (WNDPROC)WndProc;       
  87.    wc.cbClsExtra    = 0;                      
  88.    wc.cbWndExtra    = 0;                      
  89.    wc.hInstance     = hInstance;              
  90.    wc.hIcon         = LoadIcon( hInstance, lpszAppName ); 
  91.    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  92.    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  93.    wc.lpszMenuName  = lpszAppName;              
  94.    wc.lpszClassName = lpszClassName;              
  95.  
  96.    if ( IS_WIN95 )
  97.    {
  98.       if ( !RegisterWin95( &wc ) )
  99.          return( FALSE );
  100.    }
  101.    else if ( !RegisterClass( &wc ) )
  102.       return( FALSE );
  103.  
  104.    return( TRUE );
  105. }
  106.  
  107.  
  108. BOOL RegisterWin95( CONST WNDCLASS* lpwc )
  109. {
  110.    WNDCLASSEX wcex;
  111.  
  112.    wcex.style         = lpwc->style;
  113.    wcex.lpfnWndProc   = lpwc->lpfnWndProc;
  114.    wcex.cbClsExtra    = lpwc->cbClsExtra;
  115.    wcex.cbWndExtra    = lpwc->cbWndExtra;
  116.    wcex.hInstance     = lpwc->hInstance;
  117.    wcex.hIcon         = lpwc->hIcon;
  118.    wcex.hCursor       = lpwc->hCursor;
  119.    wcex.hbrBackground = lpwc->hbrBackground;
  120.    wcex.lpszMenuName  = lpwc->lpszMenuName;
  121.    wcex.lpszClassName = lpwc->lpszClassName;
  122.  
  123.    // Added elements for Windows 95.
  124.    //...............................
  125.    wcex.cbSize = sizeof(WNDCLASSEX);
  126.    wcex.hIconSm = LoadImage(wcex.hInstance, lpwc->lpszClassName, 
  127.                             IMAGE_ICON, 16, 16,
  128.                             LR_DEFAULTCOLOR );
  129.             
  130.    return RegisterClassEx( &wcex );
  131. }
  132.  
  133. LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  134. {
  135.    switch( uMsg )
  136.    {
  137.       case WM_COMMAND :
  138.               switch( LOWORD( wParam ) )
  139.               {
  140.                  case IDM_ABOUT :
  141.                         DialogBox( hInst, "AboutBox", hWnd, (DLGPROC)About );
  142.                         break;
  143.  
  144.                  case IDM_EXIT :
  145.                         DestroyWindow( hWnd );
  146.                         break;
  147.               }
  148.               break;
  149.       
  150.       case WM_DESTROY :
  151.               PostQuitMessage(0);
  152.               break;
  153.  
  154.       default :
  155.             return( DefWindowProc( hWnd, uMsg, wParam, lParam ) );
  156.    }
  157.  
  158.    return( 0L );
  159. }
  160.  
  161.  
  162. LRESULT CALLBACK About( HWND hDlg,           
  163.                         UINT message,        
  164.                         WPARAM wParam,       
  165.                         LPARAM lParam)
  166. {
  167.    switch (message) 
  168.    {
  169.        case WM_INITDIALOG: 
  170.                return (TRUE);
  171.  
  172.        case WM_COMMAND:                              
  173.                if (   LOWORD(wParam) == IDOK         
  174.                    || LOWORD(wParam) == IDCANCEL)    
  175.                {
  176.                        EndDialog(hDlg, TRUE);        
  177.                        return (TRUE);
  178.                }
  179.                break;
  180.    }
  181.  
  182.    return (FALSE); 
  183. }
  184.